package history.hot100;

// 142. 环形链表 II:https://leetcode-cn.com/problems/linked-list-cycle-ii/
// https://leetcode-cn.com/problems/linked-list-cycle-ii/solution/huan-xing-lian-biao-ii-by-leetcode-solution/
public class LeetCode_142 {
    public ListNode detectCycle(ListNode head) {
        if (head == null) return null;
        ListNode slow = head,fast = head;
        while (fast != null) {
            slow = slow.next;
            if (fast.next!=null) {
                fast = fast.next.next;
            } else {
                return null;
            }
            if (fast == slow) {
                ListNode ptr = head;
                while (ptr != slow) {
                    ptr = ptr.next;
                    slow = slow.next;
                }
                return ptr;
            }
        }
        return null;
    }
}
